﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using EventLogAnalyzer.Common;
using EventLogAnalyzer.ViewModel.XAxisInclusionStrategies;
using System.Linq;

namespace EventLogAnalyzer.ViewModel
{
    public class EventLogsGroupViewModel : ViewModelBase
    {
        public ObservableCollection<EventLogViewModel> Logs { get; set; }
        public event ShowLogGroupDetailsEventHandler ShowLogGroupDetailsRequest;

        public EventLogsGroupViewModel()
        {
            Logs = new ObservableCollection<EventLogViewModel>();    
        }

        public void AddLog(EventLogViewModel log)
        {
            Logs.Add(log);
        }


        public string XAxisValue { get; set; }

        public int LogsCount
        {
            get { return Logs.Count; }
        }

        public int LogsCountMatchingSearchCriteria
        {
            get { return Logs.Count(log => log.ShouldHighLight); }
        }

        public IXAxisInclusionStrategy InclusionStrategy { get; set; }

        public int XAxisOrder { get; set; }

        public int TotalNumOfGroups { get; set; }

        private RelayCommand _showLogGroupDetailsCommand;
        public RelayCommand ShowLogGroupDetailsCommand
        {
            get
            {
                if (_showLogGroupDetailsCommand == null)
                {
                    _showLogGroupDetailsCommand = new RelayCommand(param =>
                    {
                        if (ShowLogGroupDetailsRequest != null)
                        {
                            IEnumerable<EventLogViewModel> logsToShow;
                            if(IsNotFiltered)
                                logsToShow = Logs;
                            else
                                logsToShow = Logs.Where(l => l.ShouldHighLight).ToList();
                            ShowLogGroupDetailsRequest(this, new ShowLogGroupDetailsEventHandlerArgs() { FilteredLogs = logsToShow, DrillDown = false });
                        }
                    });
                }
                return _showLogGroupDetailsCommand;

            }
        }

        private RelayCommand _drillDownCommand;
        public RelayCommand DrillDownCommand
        {
            get
            {
                if (_drillDownCommand == null)
                {
                    _drillDownCommand = new RelayCommand(param =>
                    {
                        if (ShowLogGroupDetailsRequest != null)
                            ShowLogGroupDetailsRequest(this, new ShowLogGroupDetailsEventHandlerArgs(){FilteredLogs = Logs, DrillDown = true});

                    });
                }
                return _drillDownCommand;

            }
        }

        private bool _shouldHighLight;
        public bool ShouldHighLight
        {
            get { return _shouldHighLight; }
            set
            {
                    _shouldHighLight = value;
                    OnPropertyChanged("ShouldHighLight");
                    OnPropertyChanged("LogsCountMatchingSearchCriteria");
            }
        }

        private bool isNotFiltered = true;
        public bool IsNotFiltered
        {
            get { return isNotFiltered; }
            set
            {
                isNotFiltered = value;
                OnPropertyChanged("IsNotFiltered");
            }
        }

    }
}